#include <iostream>
#include <vector>
#include <algorithm>

void countChar(std::string input_str, std::vector<int> &cnt_list)
{
    int curr_cnt = 1;
    char curr_char = input_str[0];
    for (int i = 1; i < input_str.size(); i++)
    {
        if (curr_char != input_str[i])
        {
            if (cnt_list[curr_char - 'A'] != 0)
                cnt_list[curr_char - 'A'] = std::max(curr_cnt, cnt_list[curr_char - 'A']);
            else
                cnt_list[curr_char - 'A'] = curr_cnt;
            curr_char = input_str[i];
            curr_cnt = 1;
        }
        else
        {
            curr_cnt++;
        }
    }

    if (cnt_list[curr_char - 'A'] != 0)
        cnt_list[curr_char - 'A'] = std::max(curr_cnt, cnt_list[curr_char - 'A']);
    else
        cnt_list[curr_char - 'A'] = curr_cnt;
}

int main()
{
    std::string input_str;
    std::cin >> input_str;
    int k;
    std::cin >> k;

    std::vector<int> cnt_vec(26, 0);
    countChar(input_str, cnt_vec);

    auto cmp = [](int a, int b)
    { return a > b; };
    std::sort(cnt_vec.begin(), cnt_vec.end(), cmp);

    int i = 0;
    int c_num = 0;
    while (cnt_vec[i] != 0)
    {
        i++;
        c_num++;
    }

    if (k > c_num)
        std::cout << -1 << std::endl;
    else
        std::cout << cnt_vec[k - 1] << std::endl;
    return 0;
}